<template>
  <div>
    <el-dialog title="部门变更" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form.user_name" disabled />
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth" prop="department_id">
          <el-select v-model="form.department_id" placeholder="请选择部门">
            <el-option
              v-for="item in department"
              :key="item.id"
              :label="item.department"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFun('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadUserDepartmentApi } from '@/api/main'
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        department_id: '',
        user_name: ''
      },
      user_id: '',
      department: [],
      rules: {
        department_id: [
          { required: true, message: '请输入选择部门', trigger: 'change' }
        ]
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitFun(formname) {
      this.$refs[formname].validate((valid) => {
        if (valid) {
          const data = {
            id: this.user_id,
            department_id: this.form.department_id
          }
          uploadUserDepartmentApi(data).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.$emit('reloadFun')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
